'use strict';
app.service('promotionPosterService', ['utilService', '$q', '$http', function (utilService, $q, $http) {
    var _this_ = this;
    _this_.findById = function (id) {
        var deferred = $q.defer();
        utilService.get({
            url: $scope.app.domain + 'promotion/poster/id',
            params: {id: id}
        }).then(function (response) {
            deferred.resolve(response.data);
        }, function (e) {
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }
    _this_.download=function($scope){
        $http({
            method:'post',
            url:$scope.app.domain+'promotion/poster/download',
            data:$scope.param,
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function (data) {
            var blob = new Blob([data], {type: "image/png"});
            var fileName = "promotion_poster" + (new Date()).getTime() + '.png';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }
}])